﻿$(function () {
    $("#route-grid").datagrid({
        url: $.Api("Tour/Route/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#route-tools",
        sortName: "UpdateTime", sortOrder: "desc",
        columns:
        [[
            { field: "Name", title: "线路名称", width: 280, align: "center", sortable: true },
            {
                field: "Img", title: "封面图片", width: 120, align: "center",
                formatter: function (value, row, index) {
                    return $.GetIconButton("icon-images", "查看封面",
                        "Route.ShowImg(\"" + value + "\",\"" + row.Name + "\")");
                }
            },
            { field: "Price", title: "成人", width: 100, align: "center", sortable: true },
            { field: "Child", title: "儿童", width: 100, align: "center", sortable: true },
            { field: "Departure", title: "发团日期", width: 80, align: "center", sortable: true },
            { field: "City", title: "出发城市", width: 100, align: "center", sortable: true },
            { field: "InsertTime", title: "添加时间", width: 140, align: "center", sortable: true },
            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true },
            {
                field: "Top", title: "顶置", width: 50, align: "center", sortable: true,
                formatter: function (value, row, index) { return $.GetYesOrNo(value); }
            }
        ]]
    });

    $("#route-searchbox").searchbox({
        width: 150, prompt: "请输入线路名称查询",
        searcher: function (value) {
            $("#route-grid").datagrid("clearChecked");
            $("#route-grid").datagrid("load", { search: value });
        }
    });
});

var Route = {
    //添加
    Insert: function () {
        Route.Mutual("添加", "tour/route/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#route-grid").datagrid("getSelected");
        if (row) {
            Route.Mutual("修改", "tour/route/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#route-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Tour/Route/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Route.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 668,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    editor: "#route-content",
                    id: "#route-model",
                    url: $.Api("Tour/Route/Save"),
                    success: function (result) { Route.Reload(); }
                });
            },
            onLoad: function () { Route.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $.EditorInit("#route-content", 200, "p", true);
    },

    //分配
    Assign: function () {
        var row = $("#route-grid").datagrid("getSelected");
        if (row) {
            $.ModelDialog({
                title: "线路景点", width: 580, height: 500,
                href: "tour/route/Assign.aspx",
                onLoad: function () {
                    $("#route-scenic").datagrid({
                        url: $.Api("Tour/Scenic/Grid"),
                        nowrap: false, rownumbers: true, border: false,
                        fit: true, singleSelect: true,
                        pageSize: 10, pagination: true,
                        idField: "ID", toolbar: "#route-scenic-tools",
                        sortName: "UpdateTime", sortOrder: "desc",
                        columns:
                        [[
                            { field: "Name", title: "景点名称", width: 200, align: "center", sortable: true },
                            {
                                field: "Img", title: "封面图片", width: 120, align: "center",
                                formatter: function (v, r, i) {
                                    return $.GetIconButton("icon-images", "查看封面",
                                        "Route.ShowScenicImg(\"" + v + "\",\"" + r.Name + "\")");
                                }
                            },
                            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true },
                            {
                                field: "Top", title: "顶置", width: 50, align: "center", sortable: true,
                                formatter: function (value, row, index) { return $.GetYesOrNo(value); }
                            }
                        ]],
                        onDblClickRow: Route.InsertScenic
                    });

                    $("#route-scenic-searchbox").searchbox({
                        width: 150, prompt: "请输入景点名称查询",
                        searcher: function (value) {
                            $("#route-scenic").datagrid("clearChecked");
                            $("#route-scenic").datagrid("load", { search: value });
                        }
                    });

                    $("#route-routescenic").datagrid({
                        url: $.Api("Tour/Route/ScenicGrid"),
                        nowrap: false, rownumbers: true, border: false,
                        fit: true, singleSelect: true,
                        idField: "ID", queryParams: { id: row.ID },
                        toolbar: "#route-routescenic-tools",
                        columns:
                        [[
                            { field: "Name", title: "景点名称", width: 200, align: "center" },
                            {
                                field: "Img", title: "封面图片", width: 120, align: "center",
                                formatter: function (v, r, i) {
                                    return $.GetIconButton("icon-images", "查看封面",
                                        "Route.ShowScenicImg(\"" + v + "\",\"" + r.Name + "\")");
                                }
                            },
                            { field: "UpdateTime", title: "更新时间", width: 140, align: "center" },
                            { field: "IntSort", title: "排序", width: 50, align: "center" }
                        ]],
                        onDblClickRow: Route.DeleteScenic
                    });
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //添加景点
    InsertScenic: function () {
        var row = $("#route-scenic").datagrid("getSelected");
        if (row) {
            var rid = $("#route-grid").datagrid("getSelected").ID;
            $.AjaxMask({
                url: $.Api("Tour/Route/InsertScenic"),
                type: "post", data: { rid: rid, sid: row.ID },
                success: function (result) {
                    result = JSON.parse(result); $.EasyuiSlide(result.Message);
                    if (result.Success) { $.DataGridReload("#route-routescenic"); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //景点预览
    PreviewScenic: function () {
        var row = $("#route-scenic").datagrid("getSelected");
        if (row) {
            window.open("../Scenic.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //景点封面
    ShowScenicImg: function (file, text) {
        var img = "<img style='display:block; margin:auto;' src='../userfiles/scenic/big/" + file + "' />";
        var box = "<div style='width:" + 800 + "px;height:" + 480 + "px;'>" + img + "</div>";

        $.ModelDialog({ title: text, width: 814, iconCls: "icon-images", content: box });
    },

    //删除景点
    DeleteScenic: function () {
        var row = $("#route-routescenic").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Tour/Route/DeleteScenic"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result); $.EasyuiSlide(result.Message);
                            if (result.Success) { $.DataGridReload("#route-routescenic"); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //排序向上
    UpScenic: function () {
        Route.OrderScenic("up");
    },
    //排序向下
    DownScenic: function () {
        Route.OrderScenic("down");
    },
    //排序操作
    OrderScenic: function (order) {
        var row = $("#route-routescenic").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Tour/Route/ScenicSetOrder"), type: "post",
                data: { id: row.ID, rid: row.RouteID, order: order },
                success: function (result) {
                    result = JSON.parse(result); $.EasyuiSlide(result.Message);
                    if (result.Success) { $.DataGridReload("#route-routescenic"); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //线路景点预览
    PreviewRouteScenic: function () {
        var row = $("#route-routescenic").datagrid("getSelected");
        if (row) {
            window.open("../Scenic.aspx?id=" + row.ScenicID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //预览
    Preview: function () {
        var row = $("#route-grid").datagrid("getSelected");
        if (row) {
            window.open("../Route.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //顶置
    Top: function () {
        var row = $("#route-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Tour/Route/Top"),
                type: "post", data: { id: row.ID },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Route.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#route-grid");
    },

    //显示封面
    ShowImg: function (file, text) {
        if (!file) { text = "查看封面"; file = $("#route-img").val(); }

        var img = "<img style='display:block; margin:auto;' src='../userfiles/route/big/" + file + "' />";
        var box = "<div style='width:" + 800 + "px;height:" + 480 + "px;'>" + img + "</div>";

        $.ModelDialog({ title: text, width: 814, iconCls: "icon-images", content: box });
    },
    //上传封面
    AddImg: function () {
        KindEditor.editor({
            uploadJson: $.Api("Tour/Route/AddImg"), formatUploadUrl: false
        }).loadPlugin("image", function () {
            this.plugin.imageDialog({
                showRemote: false,
                clickFn: function (url, title, width, height, border, align) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#route-img").val(url);
                }
            });
        });
    },
    //清除封面
    DeleteImg: function () {
        $("#route-img").val("null.png"); $.EasyuiSlide("清除成功!");
    }
};